<?php

class Category_AdminController extends Ac_AdminController
{

    public function dashboardAction()
    {
        $this->view->num_active_category = Model_Category::all(array('sa' => 1))->count();
    }

    public function menuAction()
    {
        $this->view->index = $this->_getParam('index');
    }

    public function indexAction() 
    {
        $this->listAction();
    }
    
    public function listAction() 
    {
        $_GET['sort'] = $sort = !empty($_GET['sort']) ? $_GET['sort'] : 'recent';
        $page      = !empty($_GET['page']) ? $_GET['page'] : 1;
        $per_page  = 50;
        $query = '';
        $fields = array();
        $additionalParameters = array();

        if (!empty($_GET['id'])) {
            $cat = Model_Category::find($_GET['id']);
            $list = $cat->getChildren();
            $this->view->category = $cat;
        } else {
            $list = Model_Category::getLevel1(array('sa'=>array('$gte'=>0)));
        }
        //$this->view->list    = Model_Listing::all()->limit(50);
        if ($page>1) {
            $list->skip(($_GET['page']-1) * $per_page);
        }
        $this->view->list           = $list->limit($per_page)->sort(array('pos'=>1));
        $this->view->listing_number = $list->count();
        $this->view->page           = $page;
        $this->view->per_page       = $per_page;
        $this->render('list');
    }

    public function addAction()
    {
    	$level2 = array(
    		'宠物窝', '牵引绳', '宠物箱包', '宠物笼子', '宠物碗', '宠物垫', '宠物项圈', '宠物服饰', '宠物保健品'
    	
    	);
    	foreach($level2 as $k => $v){
			$_POST['val'] = $v;
	//		$_POST['tag'] = array(
	//        	'另类 ',
	//			'玩具犬',
	//			'中型犬',
	//			'大型犬',
	//			'小型犬',
	//			'公',
	//			'母',
	//			'尼龙 ',
	//			'皮质 ',
	//			'不锈钢 ',
	//			'陶瓷',
	//			'休闲'
	//        );
	        $_POST['parent'] = '4fbddc5ae8fc345810002d89';
	        $_POST['isnew'] = true;
	    	
	    	
	        $value  = $_POST['val'];
	        $cat = new Model_Category();
	        $cat->save();
	        $path = array();
	        if (!empty($_POST['parent'])) {
	            $parent_id = $_POST['parent'];
	            $parent = Model_Category::find($parent_id);
	            
	            foreach($parent->path as $par) {
	                $path[] = $par;
	            }
	        }
	        $path[] = $cat->_id;
	        $cat->path = $path;
	
	        $cat->display_website = 1;
	        $cat->homepage = 1;
	        $cat->id = md5(rand(0,1000)+time());
	        $cat->post_an_ad = 1;
	        $cat->category_search_list = 1;
	        $cat->pos = 1;
	        $cat->status = 1;
	        $cat->nm_en = $value;
	       	$cat->isnew = $_POST['isnew'];
	        $cat->tag = $_POST['tag'];
	        
	        $cat->save();
	        echo 'ok';
    	}
    	exit;
    }


    public function editAction()
    {
        
        $id = $_POST['edit_id'];
        $classname = 'Model_Category';
        /*
        if (!empty($_POST['customization'])) {
            $classname = 'Model_Category_'.$_POST['customization'];
        } else {
            $classname = 'Model_Category';
        }*/
        $class = new $classname;
        $cat = $class::getMongoCollection(false)->findOne(array('_id' => new MongoId($id)));
        //$cat   = $class::find($id);
        //$cat->_type = array('Model_Category');
        $cat['nm_en']                = $_POST['name_en'];
        $cat['nm_sc']                = $_POST['name_sc'];
        $cat['nav_nm_en']                = $_POST['nav_name_en'];
        $cat['nav_nm_sc']                = $_POST['nav_name_sc'];        
        $cat['id']                   = $_POST['handle'];
        $cat['homepage']             = (int) $_POST['homepage'];
        $cat['post_an_ad']           = (int) $_POST['post_an_ad'];
        $cat['display_website']      = (int) $_POST['display_website'];
        $cat['category_search_list'] = (int) $_POST['category_search_list'];
        $cat['url_en']               = (string) $_POST['url_en'];
        $cat['url_sc']               = (string) $_POST['url_sc'];
        $cat['so_en']               = (string) $_POST['sold_en'];
        $cat['so_sc']               = (string) $_POST['sold_sc'];        
        $cat['pt_en']                = $_POST['page_title_en'];
        $cat['pt_sc']                = $_POST['page_title_sc'];
        $cat['ds_en']                = $_POST['description_en'];
        $cat['ds_sc']                = $_POST['description_sc'];        
        $cat['bl_en']                = $_POST['blurb_en'];
        $cat['bl_sc']                = $_POST['blurb_sc'];   
        $cat['pos_big_list']         = (int)$_POST['pos_big_list'];   
        if (!empty($_POST['customization'])) {
            $cat['customization'] = strtolower($_POST['customization']);
            $cat['_type'] = array($classname, 'Model_Category');
        }
        else {
            if (empty($cat['_type'])) {
                unset($cat['customization']);
                $cat['_type'] = array('Model_Category');
            }
        }

        $class::getMongoCollection(false)->save($cat);
        echo 'ok';exit;
    }

    public function deleteAction()
    {
        $listing = Model_Category::find($_GET['id']);
        $listing->status = Model_Category::STATUS_DELETED;
        $listing->save();
        $this->_redirect($this->view->geturl('list','category', null, 'admin1', array('id'=> $_GET['back'])));
    }

    public function homepageAction()
    {
        $listing = Model_Category::find($_GET['id']);
        if (!empty($_GET['act'])) {
            $listing->homepage = 1;
        } else {
            unset($listing->homepage);
        }
        $listing->save();
        if ($this->_request->isXmlHttpRequest()) {
            echo (int)!empty($_GET['act']) ;exit;
        } else {
            $this->_redirect($this->view->geturl('list','category', null, 'admin1', array('id'=> $_GET['back'])));
        }
    }

    public function activateAction()
    {
        $listing = Model_Category::find($_GET['id']);
        $listing->status = Model_Category::STATUS_ACTIVE;
        $listing->save();
        $this->_redirect($this->view->geturl('list','category', null, 'admin1', array('id'=> $_GET['back'])));
    }

    public function positionAction()
    {
        foreach($_POST['data'] as $it) {
            $cat = Model_Category::find($it['id']);
            $cat->pos = (int)$it['pos'];
            $cat->save();
        }
        echo 'ok';exit;
    }
}
